package com.web.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.transform.TransformerException;

import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.sun.org.apache.xpath.internal.XPathAPI;

public class FileDownloadUtiles {
	
	/**
	 * 
	 * 初始化下载附件内容
	 * @param filexml
	 * @return
	 * 
	 */
	@SuppressWarnings({ "rawtypes" })
	public static List<Map> getfileDownloadMap(String filexml){
		List<Map> fileDownloadList = new ArrayList<Map>();
		if(StringUtils.isNotEmpty(filexml)){
			Document document = XMLAdapter.loadXML(filexml);
			try {
				NodeList nodelist = XPathAPI.selectNodeList(document, "Files/File");
				Map<String,String> filemap=null;
				for(int i=0;i<nodelist.getLength();i++){
					String domain = ((Element)nodelist.item(i)).getAttribute("Domain");
					String path = ((Element)nodelist.item(i)).getAttribute("Path");
					String filenamepath = ((Element)nodelist.item(i)).getAttribute("Name");
					String alias = StringUtils.isNotEmpty(((Element)nodelist.item(i)).getAttribute("Alias"))?((Element)nodelist.item(i)).getAttribute("Alias"):"common";
					String RealName = ((Element)nodelist.item(i)).getAttribute("RealName");
					String  Type = ((Element)nodelist.item(i)).getAttribute("Type");
					String  title = ((Element)nodelist.item(i)).getAttribute("Title");
					Element element = FileDomainConfig.getInstance().getOrgElement("share");
					Element domainElement = (Element) XPathAPI.selectSingleNode(element, "FileDomainList/FileDomain[@Name='"+domain+"']");
					String domainpath = domainElement.getAttribute("Path");
					domainpath = domainpath.replace("{common}", alias);
					String filepath = ParamConfig.getInstanse().getParam("fileDomainPath")+domain+(StringUtils.isNotEmpty(path)?"/"+path+"/":"/")+(StringUtils.isNotEmpty(filenamepath)?filenamepath:RealName);
					filemap = new HashMap<String,String>();
					filemap.put("name", RealName);
					filemap.put("path", filepath);
					filemap.put("alias", alias);
					filemap.put("domain", domain);
					filemap.put("type", Type);
					filemap.put("file",path);
					filemap.put("fileName", filenamepath);
					filemap.put("title", title);
					//装载原始xml内容信息
					//if(0 == i){
						//filemap.put("myfjxml", filexml);
					//}
					fileDownloadList.add(filemap);
				}
			} catch (TransformerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return fileDownloadList;
	}
	
	/**
	 * 
	 * 根据doctemplate模版获取文件物理路径
	 * @param tempfile
	 * @return String
	 * 2014年10月16日 11:12:03
	 * 
	 */
	public static String getDocTemplatePath(String tempfile){
		String docTemplatePath = "";
		if(StringUtils.isNotEmpty(tempfile)){
			Document document = XMLAdapter.loadXML(tempfile);
			try {
				NodeList nodelist = XPathAPI.selectNodeList(document, "Files/File");
				for(int i=0;i<nodelist.getLength();i++){
					String domain = ((Element)nodelist.item(i)).getAttribute("Domain");
					String path = ((Element)nodelist.item(i)).getAttribute("Path");
					String filenamepath = ((Element)nodelist.item(i)).getAttribute("Name");
					String alias = (StringUtils.isNotEmpty(((Element)nodelist.item(i)).getAttribute("Alias"))?((Element)nodelist.item(i)).getAttribute("Alias"):"common" );
					String RealName = ((Element)nodelist.item(i)).getAttribute("RealName");
					Element element = FileDomainConfig.getInstance().getOrgElement("share");
					Element domainElement = (Element) XPathAPI.selectSingleNode(element, "FileDomainList/FileDomain[@Name='"+domain+"']");
					String domainpath = domainElement.getAttribute("Path");
					domainpath = domainpath.replace("{common}", alias);
					docTemplatePath = domainpath.substring(0,domainpath.length()).replace("\\", "/")+(StringUtils.isNotEmpty(path)?"/"+path+"/":"/")+(StringUtils.isNotEmpty(filenamepath)?filenamepath:RealName);
				}
			} catch (TransformerException e) {
				e.printStackTrace();
			}
		}
		return docTemplatePath;
	}
}
